package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.SituationDao;
import cn.topevery.garden.entity.po.Situation;
import cn.topevery.garden.entity.query.SituationQuery;
import cn.topevery.garden.entity.vo.SituationVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 使用情况变更表
 *
 * @author code-generate
 * @date 2022-02-14
 */
@Repository
public class SituationDaoImpl extends BaseDaoImpl<Situation> implements SituationDao {
    @Override
    public <P extends DbCsp> List<Situation> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<SituationVO> listPage(SituationQuery query) {
        String table = "t_situation ts";

        String columns = "ts.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ts.c_id desc");
        sqlQueryMate
                .where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getHouseId()), "ts.c_house_id", ScriptConditionEnum.Like, query.getHouseId())
                .where(StringUtils.isNotBlank(query.getLandId()), "ts.c_land_id", ScriptConditionEnum.Like, query.getLandId())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, SituationVO.class);
    }

    @Override
    public Situation getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void deleteBatchById(String userId, Integer[] ids) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_situation ts");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_last_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_last_update_id", Integer.valueOf(userId))
                .where(true, "c_id", ScriptConditionEnum.In, ids);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
